package by;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * author: Mr.Du
 * Date: 2022/10/9
 * Time: 20:04
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        char[][] arr = new char[n][m];
        for(int i = 0;i < n;i++){
            String s = sc.next();
            for(int j = 0;j < m;j++){
                arr[i][j] = s.charAt(j);
            }
        }
        int[][] dp = new int[n][m];
        int res = 0;
        for(int i = 0;i < m;i++){
            if(arr[0][i] >= 65 && arr[0][i] <= 90){
                res++;
                dp[0][i] += res;
            }else {
                if(i == 0) dp[0][i] = 0;
                else dp[0][i] = dp[0][i-1];
            }
        }
        res = 0;
        for(int i = 0;i < n;i++){
            if(arr[i][0] >= 65 && arr[i][0] <= 90){
                res++;
                dp[i][0] += res;
            }else {
                if(i == 0) dp[i][0] = 0;
                else dp[i][0] = dp[i-1][0];
            }
        }
        for(int i = 1;i < n;i++){
            for(int j = 1;j < m;j++){
                if(arr[i][j] >= 65 && arr[i][j] <= 90){
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]) + 1;
                }else{
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        System.out.println(dp[n-1][m-1]);

    }
    
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] arr1 = new int[m][n];
        int[][] arr2 = new int[m][n];
        int[][] res = new int[m][n];
        for( int i = 0;i<m;i++){
            for(int j = 0;j < n;j++){
                arr1[i][j] = sc.nextInt();
            }
        }
        for( int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                arr2[i][j] = sc.nextInt();
                int sum = arr1[i][j] + 2 * arr2[i][j] - 255;
                if(sum < 0) res[i][j] = 0;
                else if(sum > 255) res[i][j] = 255;
                else res[i][j] = sum;
            }
        }
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(j < n - 1)
                    System.out.print(res[i][j] + " ");
                else
                    System.out.println(res[i][j]);
            }
        }
    }
}
